var express = require('express');
var router = express.Router();
var {getstoreById,getAllSupplier,addSupplier,getsupolierById,creatPetUser,getAllPetuser,getPetUserById,petuserUpdate,deletePetUser,getStores,changeStatus,creatAdmin, getAllAdmin, deleteAdmin, adminUpdate,getAdminById,getDorById,creatStore} = require("../servies/adminServies.js");
/* GET home page. */
router.post('/addpetuser', async function(req, res, next) {
   const data=await creatPetUser(req.body)
    res.send(data)
});
router.post('/changeStatus', async function(req, res, next) {
    const {_id,params}=req.body
   const data=await changeStatus(_id,params)
    res.send(data)
});

router.get('/getAllpetuser', async function(req, res, next) {
   const data=await getAllPetuser(req.query)
    res.send(data)
});
router.get('/getStores', async function(req, res, next) {
   const data=await getStores()
    res.send(data)
});
router.get('/getAllSupplier', async function(req, res, next) {
   const data=await getAllSupplier()
    res.send(data)
});
router.get('/getById', async function(req, res, next) {
   const data=await getPetUserById(req.query)
    res.send(data)
});
router.delete('/:_id', async function(req, res, next) {
    const {_id}=req.params
   const data=await deletePetUser({_id})
    res.send(data)
});
router.patch('/:_id', async function (req, res, next) {
    const {_id}=req.params
    const params = req.body;
     await petuserUpdate(_id, params);
    const result = await getPetUserById(_id);
    res.send(result);
});
router.patch('/update/:_id', async function (req, res, next) {
    const { _id } = req.params
    const params = req.body;
    const result = await adminUpdate(_id, params);
    res.send(result);
});

router.post('/addStore', async function (req, res, next) {
    const data = await creatStore(req.body)
    if (data._id) {
        const result= await changeStatus({_id:data.storeId},{status:2})
        res.send(result)
    }
});
router.post('/addAdmin', async function (req, res, next) {
    const data = await creatAdmin(req.body)
    res.send(data)
});
router.get('/getAllAdmin', async function (req, res, next) {
    const data = await getAllAdmin(req.query)
    res.send(data)
});
router.get('/getDorById', async function (req, res, next) {
    const data = await getDorById(req.query)
    res.send(data)
});
router.post('/deleteAd', async function (req, res, next) {
    const { _id } = req.body
    const data = await deleteAdmin({ _id })
    res.send(data)
});
router.get('/getAdminById', async function (req, res, next) {
    const data = await getAdminById(req.query)
    res.send(data)
});
router.get('/getsupolierById', async function (req, res, next) {
    const data = await getsupolierById(req.query)
    res.send(data)
});
router.get('/getstore/:id', async function (req, res, next) {
    const {id} = req.params;
    const data = await getstoreById({storeId:id})
    res.send(data)
});
router.post('/addSupplier', async function (req, res, next) {
    const data = await addSupplier(req.body)
    if (data._id) {
        const result= await changeStatus({_id:data.supplierId},{status:2})
        res.send(result)
    }
});

module.exports = router;
